package com.easpeed.comon.consts

import android.util.Log;
import com.bigyu.utils.Logger
import com.easpeed.elevator.serialport.SerialPortManager
import com.easpeed.elevator.serialport2.SerialPortManager2
import com.easpeed.elevator.utils.ByteUtil

/**
 * Created by bigyu on 2025-06-05 11:53
 * com.easpeed.comon.consts
 */
object ChannelConsts {
    val TAG = "ChannelConsts"

    // DVS 通道切换指令
    @JvmField
    val dvsWay1: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x00, 0x16)

    @JvmField
    val dvsWay2: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x01, 0x15)

    @JvmField
    val dvsWay3: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x02, 0x18)

    @JvmField
    val dvsWay4: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x03, 0x17)

    @JvmField
    val dvsWay5: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x04, 0x12)

    @JvmField
    val dvsWay6: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x05, 0x11)

    @JvmField
    val dvsWay7: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x06, 0x14)

    @JvmField
    val dvsWay8: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x07, 0x13)


    // otdr 通道切换指令
    @JvmField
    val otdrWay1: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x04, 0x12)

    @JvmField
    val otdrWay2: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x05, 0x11)

    @JvmField
    val otdrWay3: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x06, 0x14)

    @JvmField
    val otdrWay4: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x07, 0x13)

    // 红光 通道切换指令
    @JvmField
    val redWay1: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x08, 0x1E)

    @JvmField
    val redWay2: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x09, 0x1D)

    @JvmField
    val redWay3: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x10, 0x06)

    @JvmField
    val redWay4: ByteArray = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x11, 0x05)


    //    红光开命令（完成通道切换命令后执行）
    @JvmField
    val redOpenCommand: String = "aa0c004affff9c00191c0400010101002a"

    //  红光关命令（切换到DVS或者OTDR功能通道前执行）
    @JvmField
    val redCloseCommand: String = "aa0c004affffa100191c04000100010026"

    private val channelMap = hashMapOf<String, ByteArray>()
    private val otdrMap = hashMapOf<String, ByteArray>()
    private val redMap = hashMapOf<String, ByteArray>()

    @JvmStatic
    fun getDvs(channelIndex: String): ByteArray? {
        return channelMap[channelIndex] ?: channelMap["0"]
    }

    @JvmStatic
    fun getOtdr(channelIndex: String): ByteArray? {
        return otdrMap[channelIndex] ?: otdrMap["0"]
    }

    @JvmStatic
    fun getRed(channelIndex: String): ByteArray? {
        return redMap[channelIndex] ?: redMap["0"]
    }

    // 0 = dvs   1=otdr 2=red
    private var currentCommandType = 0

    /**
     * 切换OTDR通道
     */
    @JvmStatic
    fun changeOtdrChannel(channelIndex: String) {
        val data = getOtdr(channelIndex)
        try {
            currentCommandType = 1
            SerialPortManager.instance().sendData(data)
            Logger.e(TAG, "发送otdr通道切换指令成功:${ByteUtil.bytes2HexStr(data)} , 当前通道下标为：${channelIndex.toInt() + 1}")
        } catch (e: Exception) {
            Logger.e(TAG, "发送otdr通道切换指令失败:${ByteUtil.bytes2HexStr(data)} , 当前通道下标为：${channelIndex.toInt() + 1} ， error: ${e.message}")
            e.printStackTrace()
        }
    }

    @JvmStatic
    fun changeDvsChannel(channelIndex: String) {
        val data = getDvs(channelIndex)
        try {
            currentCommandType = 0
            SerialPortManager.instance().sendData(data)
            Logger.e(TAG, "发送DVS通道切换指令成功:${ByteUtil.bytes2HexStr(data)} , 当前通道下标为：${(channelIndex.toInt() + 1)}")
        } catch (e: Exception) {
            Logger.e(TAG, "发送DVS通道切换指令失败:${ByteUtil.bytes2HexStr(data)} , 当前通道下标为：${channelIndex.toInt() + 1} ， error: ${e.message}")
            e.printStackTrace()
        }
    }

    @JvmStatic
    fun changeRedChannel(channelIndex: String) {
        val data = getRed(channelIndex)
        try {
            currentCommandType = 2
            SerialPortManager.instance().sendData(data)
            Logger.e(TAG, "发送红光通道切换指令成功:${ByteUtil.bytes2HexStr(data)} , 当前通道下标为：${channelIndex.toInt() + 1}")
        } catch (e: Exception) {
            Logger.e(TAG, "发送红光通道切换指令失败:${ByteUtil.bytes2HexStr(data)} , 当前通道下标为：${channelIndex.toInt() + 1} ， error: ${e.message}")
            e.printStackTrace()
        }
    }

    @JvmStatic
    fun changeRedStatus(isOpen: Boolean) {
        val data = if (isOpen) {
            redOpenCommand
        } else {
            redCloseCommand
        }
        try {
            SerialPortManager2.instance().sendData(ByteUtil.hexStr2bytes(data))
            Logger.e(TAG, "发送红光通道开启/关闭指令成功:${data} , ")
        } catch (e: Exception) {
            Logger.e(TAG, "发送红光通道开启/关闭指令失败:${data} ,  ， error: ${e.message}")
            e.printStackTrace()
        }
    }

    /**
     * 设置事件阈值
     */
    @JvmStatic
    fun changeEventThreshold() {
        val eventThresholdStr: String = "aa18003efffffa2a071c1000%s%s%s%s50"
        try {
            val threshold = ConstsOpticalData.getThreshold()
            val a1 = ByteUtil.floatToHex(threshold.hsxs)
            val a2 = ByteUtil.floatToHex(threshold.jsmx)
            val a3 = ByteUtil.floatToHex(threshold.fsmx)
            val a4 = ByteUtil.floatToHex(threshold.ffsmx)
            val data = String.format(eventThresholdStr, a1, a2, a3, a4).toLowerCase()
            Logger.e(TAG, "changeEventThreshold:  $data")
            SerialPortManager2.instance().sendData(ByteUtil.hexStr2bytes(data))
            Logger.e(TAG, "====>发送事件阈值指令成功:${data} , ")
        } catch (e: Exception) {
            e.printStackTrace()
            Logger.e(TAG, " 发送事件阈值指令失败")
        }
    }

    init {
        channelMap.clear()
        otdrMap.clear()
        redMap.clear()



        if (CustomSetting.use8ChannelData) {
            //55 05 00 10 00 16
            channelMap["0"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x00, 0x16)
            //55 05 00 10 01 15
            channelMap["1"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x01, 0x15)
            //55 05 00 10 02 18
            channelMap["2"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x02, 0x18)
            //55 05 00 10 03 17
            channelMap["3"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x03, 0x17)
            //55 05 00 10 04 12
            channelMap["4"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x04, 0x12)
            //55 05 00 10 05 11
            channelMap["5"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x05, 0x11)
            //55 05 00 10 06 14
            channelMap["6"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x06, 0x14)
            //55 05 00 10 07 13
            channelMap["7"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x07, 0x13)


            //55 05 00 10 08 1E
            otdrMap["0"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x08, 0x1E)
            //55 05 00 10 09 1D
            otdrMap["1"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x09, 0x1D)
            //55 05 00 10 0A 20
            otdrMap["2"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x0A, 0x20)
            //55 05 00 10 0B 1F
            otdrMap["3"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x0B, 0x1F)
            //55 05 00 10 0C 1A
            otdrMap["4"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x0C, 0x1A)
            //55 05 00 10 0D 19
            otdrMap["5"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x0D, 0x19)
            //55 05 00 10 0E 1C
            otdrMap["6"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x0E, 0x1C)
            //55 05 00 10 0F 1B
            otdrMap["7"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x0F, 0x1B)


            //            55 05 00 10 10 06
            redMap["0"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x10, 0x06)
            //55 05 00 10 11 05
            redMap["1"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x11, 0x05)
            //55 05 00 10 12 08
            redMap["2"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x12, 0x08)
            //55 05 00 10 13 07
            redMap["3"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x13, 0x07)
            //55 05 00 10 14 02
            redMap["4"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x14, 0x02)
            //55 05 00 10 15 01
            redMap["5"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x15, 0x01)
            //55 05 00 10 16 04
            redMap["6"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x16, 0x04)
            //55 05 00 10 17 03
            redMap["7"] = byteArrayOf(0x55, 0x05, 0x00, 0x10, 0x17, 0x03)
        } else {

            channelMap["0"] = dvsWay1
            channelMap["1"] = dvsWay2
            channelMap["2"] = dvsWay3
            channelMap["3"] = dvsWay4

            otdrMap["0"] = otdrWay1
            otdrMap["1"] = otdrWay2
            otdrMap["2"] = otdrWay3
            otdrMap["3"] = otdrWay4

            redMap["0"] = redWay1
            redMap["1"] = redWay2
            redMap["2"] = redWay3
            redMap["3"] = redWay4
        }


    }


}